package longrise.phone.com.bjjt_henan.service;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.loaddata.newloaddata.LoadDataManager4;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;

import java.util.Timer;
import java.util.TimerTask;

import longrise.phone.com.bjjt_henan.BuildConfig;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;

/**
 * Created by Administrator on 2017/6/26.
 */

public class LocationService extends Service implements LocationHelper.LocationCallBack {

    LocationHelper helper;
    Timer timer;
    public static String LOCATION = "LOCATION";

    private String methodName;
    private String baseurl;
    private String uid;
    private String token;

    @Override
    public void onCreate() {
        super.onCreate();
        helper = LocationHelper.getInstance(getApplication());
        helper.setLocationCallBack(this);
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Bundle bundleExtra = intent.getBundleExtra(LOCATION);
        methodName = bundleExtra.getString("method", "");
        uid = bundleExtra.getString("uid", "");
        token = bundleExtra.getString("token", "");
        baseurl = bundleExtra.getString("baseurl", "");

        if (TextUtils.isEmpty(methodName) || TextUtils.isEmpty(uid) || TextUtils.isEmpty(token) || TextUtils.isEmpty
                (baseurl)) {
        } else {
            if (null == timer) {
                timer = new Timer();

                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        helper.start();
                    }
                }, 0, 300000);
            }
        }

        return START_REDELIVER_INTENT;
    }

    @Override
    public void LocationcallBack(String Latitude, String Longitude) {
        if (Latitude.equals("4.9E-324") || Longitude.equals("4.9E-324")) {
            return;
        }
        loadData(Latitude, Longitude);
    }


    public void loadData(String Latitude, String Longitude) {
        EntityBean bean = new EntityBean();
        bean.set("maplon", Longitude);
        bean.set("maplat", Latitude);
        bean.set("uid", uid);
        bean.set("token", token);
        LoadDataManager4.getInstance(getApplication()).callService("", baseurl, methodName, bean, new LoadDataManagerFather.OnRequestCompleteListener() {
            @Override
            public void onSuccess(String key, String service, Object result) {
                if (BuildConfig.DEBUG) {
                    Log.i("hnkckpgetlocation", "onSuccess--->" + CommontUtils.getNowTime());
                }
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

            @Override
            public void onFinished(String key, String service) {

            }
        });
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != helper) {
            helper.destroy();
        }
        if (null != timer) {
            timer.cancel();
            timer = null;
        }
    }
}



